package cn.xuqiudong.common.blog.search.config;

import cn.xuqiudong.common.blog.search.helper.SearchBlogHelper;
import cn.xuqiudong.common.blog.search.service.SearchBlogService;
import com.meilisearch.sdk.Client;
import com.meilisearch.sdk.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 描述:
 *   MeiliSearch配置,
 * 可以通过@Import 导入本配置
 * link: https://github.com/meilisearch/meilisearch-java
 *
 * @author Vic.xu
 * @since 2024-06-06 16:35
 */
@Configuration
public class MeiliSearchConfig {

    @Value("${search.enabled:false}")
    private boolean enable;

    @Value("${meilisearch.host:http://localhost:7700}")
    private String host;

    @Value("${meilisearch.key:default}")
    private String key;

    @Value("${search.blog.indexName:articles}")
    private String indexName;

    @Bean
    public Client client() {
        return new Client(config());
    }

    @Bean
    public Config config() {
        return new Config(host, key);
    }

    @Bean
    public SearchBlogService searchBlogService() {
        return new SearchBlogService(searchBlogHelper(client()));
    }
    @Bean
    public SearchBlogHelper searchBlogHelper(Client client) {
        return new SearchBlogHelper(enable, client, indexName);
    }

}
